//
// Description: 169. 多数元素
// Created by Loading on 2025/2/18.
//

#include <bits/stdc++.h>

using namespace std;

int majorityElement(vector<int> &nums) {
    int x = 0, count = 0;
    for (auto &i : nums) {
        // 超过一半的数时，count会大于0，x为此数
        if (count == 0) {
            x = i;
            ++count;
        } else {
            if (x != i) {
                --count;
            } else {
                ++count;
            }
        }
    }

    return x;
}

int main() {
    vector<int> v{2, 2, 1, 1, 1, 2, 2};
    auto res = majorityElement(v);

    cout << res << endl;

    return 0;
}
